import uuid

from django.contrib.contenttypes.fields import GenericRelation
from django.db import IntegrityError
from django.db import models

from rest_framework.pagination import PageNumberPagination

from apps.system.models import Check
from extensions.models import *

class Pagination(PageNumberPagination):
    page_size = 15
    page_size_query_param = 'page_size'
    page_query_param = 'current'
    max_page_size = 100000


class PbiBaseModel(Model):
    """基本类"""
    number = CharField(max_length=256, verbose_name='编号')
    name = CharField(max_length=256, verbose_name='名称')
    contact = CharField(max_length=256, null=True, blank=True, verbose_name='联系人')
    phone = CharField(max_length=256, null=True, blank=True, verbose_name='手机号')
    email = CharField(max_length=256, null=True, blank=True, verbose_name='邮箱')
    remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
    step = IntegerField(verbose_name='填报步骤', null=True, blank=True)
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除
    is_effective  = IntegerField(default=0,null=True, blank=True, verbose_name='状态')#状态-1=待修改0=待审核1=已生效
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间',null=True, blank=True)
    last_random_number = 0

    class Meta:
        abstract=True

class PbiInfoBaseModel(Model):
    """Pbi项目信息管理基本类"""
    company = ForeignKey('system.Company', on_delete=CASCADE, blank=True, null=True, default=None,related_name='%(class)s_company')  # 填报公司
    number = CharField(max_length=256, verbose_name='编号',unique=True)
    name = CharField(max_length=256, verbose_name='名称')
    remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
    step = IntegerField(verbose_name='填报步骤', null=True, blank=True)
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除
    is_effective  = IntegerField(default=0,null=True, blank=True, verbose_name='状态')#状态-1=待修改0=待审核1=已生效
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间',null=True, blank=True)
    creator = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None, verbose_name='创建者',related_name='%(class)s_creator')
    updater = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None, verbose_name='更新者',related_name='%(class)s_updater')
    next_checker = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None,related_name='%(class)s_next_checker')  # 下一步审核或操作的人

    class Meta:
        abstract=True


# 七库全书基本类
class ClsbbBaseModel(Model):
    number = CharField(max_length=255, unique=True, verbose_name='编号', null=True, blank=True)
    name = CharField(max_length=255, verbose_name='名称', null=True, blank=True)
    downloads = IntegerField(verbose_name='下载量',default=0, null=True, blank=True)
    views = IntegerField(verbose_name='浏览量', default=0,null=True, blank=True)

    step = IntegerField(verbose_name='填报步骤', null=True, blank=True)

    is_effective = IntegerField(default=0, null=True, blank=True, verbose_name='是否生效')  # 1=已生效0=未生效-1=退回修改
    add_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    remarks = CharField(max_length=255, verbose_name='备注', null=True, blank=True)
    class Meta:
        indexes = [
            Index(fields=['id']),
        ]
        abstract=True

# PAM基本类
class PamBaseModel(Model):
    number = CharField(max_length=255, unique=True, verbose_name='编号', null=True, blank=True)
    name = CharField(max_length=255, verbose_name='名称', null=True, blank=True)
    step = IntegerField(verbose_name='填报步骤', null=True, blank=True)
    is_effective = IntegerField(default=0, null=True, blank=True, verbose_name='是否生效')  # 1=已生效0=未生效-1=退回修改
    add_time = DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    update_time = DateTimeField(verbose_name='更新时间', null=True, blank=True)
    del_flag = BooleanField(verbose_name='删除标识', default=False)
    remarks = CharField(max_length=255, verbose_name='备注', null=True, blank=True)
    class Meta:
        indexes = [
            Index(fields=['id']),
        ]
        abstract=True




class PbiBaseCheck(Model):
    opinion = CharField(max_length=1024, verbose_name='意见', null=True, blank=True)
    is_agree = BooleanField(null=True, blank=True,default=True)
    add_time =DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        abstract=True


class Options(Model):
    name = CharField(max_length=255, unique=True, blank=True,verbose_name='该名称')
    add_time =DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)

    class Meta:
        verbose_name = '选项'
        verbose_name_plural = verbose_name
        abstract=True

    def __str__(self):
        return self.name


class OpocBaseModel(Model):
    id = CharField(max_length=32, primary_key=True)
    slug = models.SlugField(unique=True, max_length=32,null=True, blank=True)
    name = CharField(max_length=255, null=True, blank=True,verbose_name='该名称')
    add_time =DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)
    update_time = DateTimeField(verbose_name='更新时间', null=True, blank=True)
    del_flag = BooleanField(verbose_name='删除标识', default=False)
    remarks = CharField(max_length=255, verbose_name='备注', null=True, blank=True)
    class Meta:
        verbose_name = 'OpocBaseModel'
        verbose_name_plural = verbose_name
        abstract=True

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        while not self.id:
            try:
                self.id = uuid.uuid1().int >> 64
                self.slug = str(uuid.uuid4()).replace("-", "")
                super().save(*args, **kwargs)
            except IntegrityError:
                self.id = None


class OpocBaseModelNew(Model):
    # id = models.BigAutoField(primary_key=True)
    slug = models.SlugField(unique=True, max_length=32)
    name = CharField(max_length=255,null=True, blank=True,verbose_name='该名称')
    add_time =DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)
    update_time = DateTimeField(verbose_name='更新时间', null=True, blank=True)
    del_flag = BooleanField(verbose_name='删除标识', default=False)
    remarks = CharField(max_length=255, verbose_name='备注', null=True, blank=True)
    class Meta:
        abstract=True

    def __str__(self):
        return self.name


class CbspBaseModel(Model):
    remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
    step = IntegerField(verbose_name='填报步骤', null=True, blank=True)
    del_flag = BooleanField(default=False, null=True, blank=True, verbose_name='删除标志')  # 0=未删除1=已删除
    is_effective  = IntegerField(default=0,null=True, blank=True, verbose_name='状态')#状态-1=待修改0=待审核1=已生效
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间',null=True, blank=True)
    update_time = DateTimeField(auto_now=True, verbose_name='更新时间',null=True, blank=True)
    creator = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None, verbose_name='创建者',related_name='%(class)s_creator',db_constraint=False)
    updater = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None, verbose_name='更新者',related_name='%(class)s_updater',db_constraint=False)
    next_checker = ForeignKey('system.User', on_delete=CASCADE, blank=True, null=True, default=None,related_name='%(class)s_next_checker',db_constraint=False)  # 下一步审核或操作的人
    checks = GenericRelation(Check)
    class Meta:
        abstract=True




__all__ = [
    'PbiBaseModel',
    'PbiBaseCheck',
]
